<template>
  <div style="height: 100%">
    <el-card>
      <h2 style="text-align: center;margin-bottom: 20px">数据查询及筛选</h2>
      <el-row style="line-height: 40px;text-align: center;margin-bottom: 20px">
        <el-col :span="2">
          姓名:
        </el-col>
        <el-col :span="3">
          <el-input v-model="searchData.name" placeholder="请输入内容"></el-input>
        </el-col>
        <el-col :span="2">
          性别:
        </el-col>
        <el-col :span="3">
          <el-select v-model="searchData.sex">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-col>
        <el-col :span="2">
          年龄:
        </el-col>
        <el-col :span="3">
          <el-input v-model="searchData.age" placeholder="请输入内容"></el-input>
        </el-col>
        <el-col :span="2">
          是否运动员:
        </el-col>
        <el-col :span="3">
          <el-select v-model="searchData.isAthlete">
            <el-option label="是" value="是"></el-option>
            <el-option label="否" value="否"></el-option>
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" style="width: 100px">搜索</el-button>
        </el-col>
      </el-row>

      <el-table
          :data="tableData"
          stripe
          header-cell-class-name="headerCellStyle"
          cell-class-name="cellStyle"
          border
          height="calc(100% - 120px)"
          style="width: 100%;">
        <el-table-column label="ID" prop="id" width="50"></el-table-column>
        <el-table-column label="姓名" prop="name"></el-table-column>
        <el-table-column label="性别" prop="sex"></el-table-column>
        <el-table-column label="年龄" prop="age"></el-table-column>
        <el-table-column label="身高" prop="height"></el-table-column>
        <el-table-column label="体重" prop="weight"></el-table-column>
        <el-table-column label="BMI" prop="BMI"></el-table-column>
        <el-table-column label="是否运动员" prop="isAthlete"></el-table-column>
        <el-table-column label="测试日期" prop="date"></el-table-column>
        <el-table-column label="预测VO2max" prop="predict"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="addEcharts(scope.row)">加入对比</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div id="main" style="width: 100%;height: 500px;margin-top: 20px"></div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "search",
  data() {
    return {
      searchData: {
        name: '',
        sex: '',
        age: '',
        isAthlete: ''
      },
      tableData: [
        {
          id: 1,
          name: '张三',
          sex: '男',
          age: 20,
          height: 180,
          weight: 75,
          BMI: 25,
          date: '2022-01-01',
          isAthlete: '是',
          predict: 222,
          value1: 10,
          value2: 15,
          value3: 20,
          value4: 25
        },
        {
          id: 2,
          name: '李四',
          sex: '男',
          age: 22,
          height: 166,
          weight: 70,
          BMI: 22,
          date: '2024-02-17',
          isAthlete: '是',
          predict: 223,
          value1: 12,
          value2: 28,
          value3: 23,
          value4: 16
        }
      ],
      contrastData: []
    }
  },
  created(){

  },
  mounted() {
    this.setEcharts()
  },
  methods: {
    setEcharts() {
      let nameArr = []
      let valueArr = []
      this.contrastData.forEach(item => {
        nameArr.push(item.name)
        valueArr.push({
          name: item.name,
          type:'line',
          data:[item.value1, item.value2, item.value3, item.value4]
        })
      })
      //获取实例
      let myEchart = this.$echarts.init(document.getElementById("main"));
      //echarts配置项
      let option = {
        title: {
          text: "对比图",
        },
        tooltip: {},
        legend: {
          data: nameArr,
        },
        xAxis: {
          data: ["类别1", "类别2", "类别3", "类别4"],
        },
        yAxis: {},
        series: valueArr,
      };
      myEchart.setOption(option);
    },
    addEcharts(val) {
      let temp = this.contrastData.filter(item => item.id === val.id)
      if (temp.length !== 0) {
        return
      }
      this.contrastData.push(val)
      this.setEcharts()
    }
  }
}
</script>

<style scoped>
/deep/ .cellStyle {
  text-align: center;
}

/deep/ .headerCellStyle {
  text-align: center;
  background: #F8F8F8 !important;
  color: #444;
  height: 50px;
}
</style>
